home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 5 / Eagles_Nest_Mac_Collection_Disc_5.TOAST / Math & Engineering / MacBrain110 / LoadAndSave.p < prev    next >
Text File  |  1987-10-21  |  10KB  |  348 lines

  1. unit LoadAndSave;
  2.  
  3. INTERFACE
  4.  
  5. USES
  6.     Memtypes, Quickdraw, OSIntf, ToolIntf,  PackIntf, PasLibIntf, SANE;
  7.  
  8. {To use this unit, you must declare global variables to store the network}
  9. {in.  Use Appendix A in your MacBrain™ manual to check which data types }
  10. {are necessary.  We didn't include these declarations; this allows you }
  11. {more freedom in designing your application.  You also need the following}
  12. {functions:                                                                }
  13. {    GetWeight(y,x:integer):real            returns weight from unit y to unit x}
  14. {    GetMLinkUnit(y,x:integer):integer        returns unit that modulates weight from unit y to unit x}
  15. {    GetMLinkValue(y,x:integer):real            returns value of m-link on weight from unit y to unit x    }
  16. {and the following procedures:                                            }
  17. {    SetWeight(y,x:integer; weight:real)            sets weight from unit y to unit x to weight}
  18. {    SetMLink(y,x,my:integer; mweight:real)     sets m-link unit on weight from unit y to unit x to be}
  19. {                                                                    equal to my, and the m-link value equal to mweight.}
  20. {In addition, you should add a procedure 'note(message:str255)' that will}
  21. {display an alert containing 'message'.  This is not crucial, however.}
  22. {These routines will give the window 'TheWindow' a title equivalent to }
  23. {the most-recently loaded or saved file name.}
  24.  
  25. VAR
  26.     theReply : SFReply;
  27.     theMessage:integer;
  28.     nDocs: integer;
  29.     countDocs:integer;
  30.     docInfo:AppFile;
  31.     
  32. PROCEDURE actually_save;        {Used by save_file.}
  33. PROCEDURE save_file;            {Call this from your file menu.}
  34. PROCEDURE actually_load;        {Used by load_file.}
  35. PROCEDURE load_file;            {Call this from your file menu.}
  36. PROCEDURE open_from_finder;        {Opens a file if application was launched}
  37.                                 {from finder by opening the file.}
  38.  
  39.  
  40. IMPLEMENTATION
  41.  
  42. PROCEDURE actually_save;
  43.     var 
  44.         resultCode:OSErr;
  45.         theInfo : FInfo;
  46.         x,y:integer;
  47.     BEGIN
  48.         if file_name<>'Untitled' then
  49.         begin
  50.             rewrite(file_of_units, file_name);
  51.             writeln(file_of_units, '110');
  52.             writeln(file_of_units, total_groups);
  53.             FOR x := 1 TO total_groups DO
  54.                 writeln(file_of_units, group_names[x]);
  55.             writeln(file_of_units, DefaultActFunction);
  56.             writeln(file_of_units, LearnDef);
  57.             writeln(file_of_units, DefaultGroup);
  58.             writeln(file_of_units, DefaultThreshold);
  59.             writeln(file_of_units, DefaultActivation);
  60.             writeln(file_of_units, DefaultDecay);
  61.             writeln(file_of_units, DefaultInstrument);
  62.             writeln(file_of_units, DefaultWeightDisplay);
  63.             writeln(file_of_units, DefaultUpdateFrequency);
  64.             IF DefaultDisplayNames THEN
  65.                 writeln(file_of_units, 'T')
  66.             ELSE
  67.                 writeln(file_of_units, 'F');
  68.             IF DefaultSymLinks THEN
  69.                 writeln(file_of_units, 'T')
  70.             ELSE
  71.                 writeln(file_of_units, 'F');
  72.             IF DefaultClamp THEN
  73.                 writeln(file_of_units, 'T')
  74.             ELSE
  75.                 writeln(file_of_units, 'F');
  76.             writeln(file_of_units, total_units);
  77.             FOR x := 1 TO total_units DO
  78.                 WITH units[x]^ DO
  79.                     BEGIN
  80.                         writeln(file_of_units, name);
  81.                         writeln(file_of_units, instrument);
  82.                         writeln(file_of_units, group);
  83.                         writeln(file_of_units, locX);
  84.                         writeln(file_of_units, locY);
  85.                         writeln(file_of_units, threshold);
  86.                         writeln(file_of_units, decay);
  87.                         writeln(file_of_units, activation);
  88.                         writeln(file_of_units, activation2);
  89.                         writeln(file_of_units, last_activation);
  90.                         writeln(file_of_units, output);
  91.                         writeln(file_of_units, other_thing);
  92.                         writeln(file_of_units, clamped);
  93.                         writeln(file_of_units, update_frequency);
  94.                         IF boolean1 THEN
  95.                             writeln(file_of_units, 'T')
  96.                         ELSE
  97.                             writeln(file_of_units, 'F');
  98.                         IF boolean2 THEN
  99.                             writeln(file_of_units, 'T')
  100.                         ELSE
  101.                             writeln(file_of_units, 'F');
  102.                         FOR y := 1 TO total_units DO
  103.                             IF GetWeight(y,x) <> 0 THEN
  104.                                 BEGIN
  105.                                     writeln(file_of_units, y);
  106.                                     writeln(file_of_units, GetWeight(y,x));
  107.                                     writeln(file_of_units, GetMLinkUnit(y,x));
  108.                                     writeln(file_of_units, GetMLinkValue(y,x));
  109.                                 END;
  110.                         writeln(file_of_units, 0);
  111.                         writeln(file_of_units, 0.0); 
  112.                         writeln(file_of_units, 0);
  113.                         writeln(file_of_units, 0.0);     { end of weights }
  114.                     END;                                     { end units loop }
  115.             close(file_of_units);
  116.             theInfo.fdType := 'NNE1';
  117.             theInfo.fdCreator := 'MNJ1';
  118.             resultCode := SetFInfo(theReply.fName, theReply.vRefNum, theInfo);
  119.             IOCheck(resultCode);
  120.             SetWTitle(TheWindow, file_name);
  121.             InitCursor;
  122.         end;
  123.     END;
  124.  
  125.     PROCEDURE save_file;
  126.         CONST
  127.             wrongTypeID = 1004;
  128.         VAR
  129.             x, y, zz : integer;
  130.             dlgOrigin : Point;
  131.             string1 : STRING[70];
  132.             MyParamBlock: WDPBPtr;
  133.             theDirRefNum:integer;
  134.             MyRefNum:integer;
  135.             theFile : integer;
  136.             ignore : integer;
  137.             theInfo : FInfo;
  138.             resultCode : OSErr;
  139.  
  140.     BEGIN
  141.         SetPt(dlgOrigin, DlgLeft, DlgTop);
  142.         theTypeList[0] := 'NNE1';
  143.         if file_name='Untitled' then
  144.         SFPutFile(dlgOrigin, 'Save model as what ?', 'model name', NIL, theReply)
  145.         else
  146.         SFPutFile(dlgOrigin, 'Save model as what ?', file_name, NIL, theReply);
  147.         if theReply.fname<>'Untitled' then
  148.             WITH theReply DO
  149.                 BEGIN
  150.                     IF good THEN
  151.                         BEGIN
  152.                             SetCursor(Watch^^);
  153.                             new(MyParamBlock);
  154.                             with MyParamBlock^ do
  155.                                 begin
  156.                                     ioNamePtr:=NIL;
  157.                                     ioVRefNum:=vRefNum;
  158.                                     ioWDDirID:=0;{?}
  159.                                 end;
  160.                             resultCode:=PBHSetVol(MyParamBlock,FALSE);
  161.                             if resultcode<>0 then note('Error in setting directory.');
  162.                             file_name := fName;
  163.                             actually_save;
  164.                             EnableItem(MyFileMenu,fmsave);
  165.                         END;
  166.                 END;
  167.     END;
  168.  
  169.  
  170.  
  171.     PROCEDURE new_file;
  172.         VAR
  173.             x : integer;
  174.             resultCode : OSErr;
  175.             disposeError : boolean;
  176.     BEGIN
  177.         disposeError := false;
  178.         x := 1;
  179.         WHILE ((x <= total_units) AND (NOT disposeError)) DO
  180.             BEGIN
  181.                 dispose(units[x]);
  182.                 resultCode := MemError;
  183.                 IF resultCode <> 0 THEN
  184.                     stop('Dispose Error in New_File');
  185.                 IF resultCode <> 0 THEN
  186.                     disposeError := true;
  187.                 x := x + 1;
  188.             END;
  189.         total_units := 0;
  190.         SetPort(TheWindow);
  191.         view_update;
  192.         file_name:='Untitled';
  193.         SetWTitle(TheWindow, 'Untitled');
  194.         DisableItem(MyFileMenu,fmsave);
  195.     END;
  196.  
  197.     PROCEDURE actually_load;
  198.         VAR
  199.             x, y,my, z : integer;
  200.             weight,mweight : real;
  201.             ch : char;
  202.             string1 : STRING[70];
  203.             resultCode : OSErr;
  204.             MyParamBlock: WDPBPtr;
  205.             theDirRefNum:integer;
  206.             MyRefNum:integer;
  207.             theFile : integer;
  208.     BEGIN
  209.             BEGIN
  210.                 BEGIN
  211.                     SetCursor(Watch^^);
  212.                     new(MyParamBlock);
  213.                     with MyParamBlock^ do
  214.                         begin
  215.                             ioNamePtr:=NIL;
  216.                             ioVRefNum:=theReply.vRefNum;
  217.                             ioWDDirID:=0;{?}
  218.                         end;
  219.                     resultCode:=PBHSetVol(MyParamBlock,FALSE);
  220.                     if resultcode<>0 then note('Error in setting directory.');
  221.                     x := total_units;
  222.                     IF x > 0 THEN
  223.                         new_file;
  224.                     file_name := theReply.fName;
  225.                     reset(file_of_units, file_name);
  226.                     readln(file_of_units, total_groups);
  227.                     readln(file_of_units, total_groups);
  228.                     FOR x := 1 TO total_groups DO
  229.                         readln(file_of_units, group_names[x]);
  230.                     readln(file_of_units, DefaultActFunction);
  231.                     readln(file_of_units, LearnDef);
  232.                     readln(file_of_units, DefaultGroup);
  233.                     readln(file_of_units, DefaultThreshold);
  234.                     readln(file_of_units, DefaultActivation);
  235.                     readln(file_of_units, DefaultDecay);
  236.                     readln(file_of_units, DefaultInstrument);
  237.                     readln(file_of_units, DefaultWeightDisplay);
  238.                     readln(file_of_units, DefaultUpdateFrequency);
  239.                     {readln(file_of_units, total_graphs);}
  240.                     {for z:=1 to total_graphs do}
  241.                     {    readln(file_of_units, DefaultGraph[z]);}
  242.                     readln(file_of_units, ch);
  243.                     IF ch = 'T' THEN
  244.                         DefaultDisplayNames := True
  245.                     ELSE
  246.                         DefaultDisplayNames := False;
  247.                     readln(file_of_units, ch);
  248.                     IF ch = 'T' THEN
  249.                         DefaultSymLinks := True
  250.                     ELSE
  251.                         DefaultSymLinks := False;
  252.                     readln(file_of_units, ch);
  253.                     IF ch = 'T' THEN
  254.                         DefaultClamp := True
  255.                     ELSE
  256.                         DefaultClamp := False;
  257.                     readln(file_of_units, total_units);
  258.                     FOR x := 1 TO total_units DO
  259.                         new(units[x]);
  260.                     FOR x := 1 TO total_units DO
  261.                         BEGIN
  262.                             WITH units[x]^ DO
  263.                                 BEGIN
  264.                                     readln(file_of_units, name);
  265.                                     readln(file_of_units, instrument);
  266.                                     readln(file_of_units, group);
  267.                                     readln(file_of_units, locX);
  268.                                     readln(file_of_units, locY);
  269.                                     readln(file_of_units, threshold);
  270.                                     readln(file_of_units, decay);
  271.                                     readln(file_of_units, activation);
  272.                                     readln(file_of_units, activation2);
  273.                                     readln(file_of_units, last_activation);
  274.                                     readln(file_of_units, output);
  275.                                     readln(file_of_units, other_thing);
  276.                                     readln(file_of_units, clamped);
  277.                                     readln(file_of_units, update_frequency);
  278.                                     readln(file_of_units, ch);
  279.                                     IF ch = 'T' THEN
  280.                                         boolean1 := True
  281.                                     ELSE
  282.                                         boolean1 := False;
  283.                                     readln(file_of_units, ch);
  284.                                     IF ch = 'T' THEN
  285.                                         boolean2 := True
  286.                                     ELSE
  287.                                         boolean2 := False;
  288.                                     WeightBase:=NIL;
  289.                                     y := 1;
  290.                                     WHILE y <> 0 DO
  291.                                         BEGIN
  292.                                             readln(file_of_units, y);
  293.                                             readln(file_of_units, weight);
  294.                                             readln(file_of_units, my);
  295.                                             readln(file_of_units, mweight);
  296.                                             IF y <> 0 THEN
  297.                                                 BEGIN
  298.                                                     SetWeight(y,x,weight);
  299.                                                     SetMLink(y,x,my,mweight);
  300.                                                 END;
  301.                                         END;
  302.                                 END;
  303.                         END;
  304.                 END;
  305.                 close(file_of_units);
  306.                 SetWTitle(TheWindow, file_name);
  307.                 EnableItem(MyFileMenu,fmsave);
  308.             END;
  309.     END;
  310.     
  311.     PROCEDURE load_file;
  312.         VAR
  313.             dlgOrigin : Point;
  314.     BEGIN
  315.         SetPt(dlgOrigin, DlgLeft, DlgTop);
  316.         theTypeList[0] := 'NNE1';
  317.         SFGetFile(dlgOrigin, 'Load which model ?', NIL, 1, theTypeList, NIL, theReply);
  318.         IF theReply.good THEN
  319.             actually_load;
  320.     END;
  321.  
  322. PROCEDURE open_from_finder;
  323. BEGIN
  324.     CountAppFiles(theMessage,nDocs);
  325.     if nDocs>0 then
  326.     BEGIN
  327.         if theMessage=AppPrint then
  328.             ExitToShell
  329.         else
  330.             begin
  331.                 GetAppFiles(1,docInfo);
  332.                 if docInfo.fType='NNE1' then
  333.                     begin
  334.                         theReply.fName:=docInfo.fName;
  335.                         theReply.vRefNum:=docInfo.vRefNum;
  336.                         actually_load;
  337.                         for countDocs:= 1 to nDocs do ClrAppFiles(CountDocs);
  338.                     end
  339.                 else
  340.                     begin
  341.                         Stop('File must be converted to 1.10 format.');
  342.                         ExitToShell;
  343.                     end;
  344.             end;
  345.     END;
  346. End; {open_from_finder}
  347.  
  348. END. {implementation}